package com.moli.iov.down.command.codec.decode.decoders;

import com.moli.iov.down.command.codec.decode.bean.QueryResBean;
import com.moli.iov.down.command.codec.decode.bean.extend.QueryResExtendBean;
import com.moli.iov.down.command.design.command.param.query.ParamQueryCommand;
import com.moli.iov.down.command.design.factory.param.ParamQueryCommandFactory;
import com.moli.iov.util.ByteArrayUtil;
import com.moli.iov.util.StringUtil;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  终端参数查询结果上报应答  解码
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/30 18:18
 */
@Component("queryDecoder")
@Scope("prototype")
public class QueryDecoder extends BaseDecoder{

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    public QueryResBean decode(byte[] data){
        log.info("终端参数查询结果上报应答  解码 入参：{}", data);
        byte[] date = ArrayUtils.subarray(data, 0, 6);
        data = ArrayUtils.subarray(data, 6, data.length);

        byte[] serialNumber = ArrayUtils.subarray(data, 0, 2);
        data = ArrayUtils.subarray(data, 2, data.length);

        int num = ByteArrayUtil.toInt(ArrayUtils.subarray(data, 0, 1));
        data = ArrayUtils.subarray(data, 1, data.length);

        List<QueryResExtendBean> queryResExtendBeans = this.extendDecode(num, data);

        QueryResBean queryResBean = new QueryResBean();
        queryResBean.setDate(this.dateDecode(date));
        queryResBean.setSerialNumber(this.serialNumberDecode(serialNumber));

        queryResBean.setQueryResExtendBeans(queryResExtendBeans);

        log.info("终端参数查询结果上报应答  解码 结果: {}", queryResBean);
        return queryResBean;
    }

    /**
     * 解析参数
     * @param num
     * @param paramDatas
     * @return
     */
    private List<QueryResExtendBean> extendDecode(int num, byte[] paramDatas){
        List<QueryResExtendBean> queryResExtendBeans = new ArrayList<>();
        for(int i = 0; i < num; i++){
            byte[] paramId = ArrayUtils.subarray(paramDatas, 0, 2);
            paramDatas = ArrayUtils.subarray(paramDatas, 2, paramDatas.length);

            int len = ByteArrayUtil.toInt(ArrayUtils.subarray(paramDatas, 0, 1));
            paramDatas = ArrayUtils.subarray(paramDatas, 1, paramDatas.length);

            byte[] value = ArrayUtils.subarray(paramDatas, 0, len);
            paramDatas = ArrayUtils.subarray(paramDatas, len, paramDatas.length);


            QueryResExtendBean queryResExtendBean = new QueryResExtendBean();
            String paramIdStr = StringUtil.bytesToHexString(paramId);
            queryResExtendBean.setParamId(paramIdStr);
            ParamQueryCommand paramQueryCommand =
                    ParamQueryCommandFactory.getParamQueryCommand(paramIdStr);
            queryResExtendBean = paramQueryCommand.execute(queryResExtendBean, value);

            queryResExtendBeans.add(queryResExtendBean);
        }
        return queryResExtendBeans;
    }
}
